#include <iostream>
#include <memory>

#include "TcpServer.hpp"

using namespace tcpserver_ns;
using namespace std;

static void usage(char *argv)
{
    std::cout << "Usage:\n\t" << argv << " serverPort" << std::endl;
}

Responce calculate(const Request &req)
{
    Responce resq;
    switch (req._op)
    {
    case '+':
        resq._result = req._x + req._y;
        break;
    case '-':
        resq._result = req._x - req._y;
        break;
    case '*':
        resq._result = req._x * req._y;
        break;
    case '/':
        if(req._y==0)
            resq._code=1;
        else
            resq._result = req._x / req._y;
        break;
    case '%':
        if(req._y==0)
            resq._code=2;
        else
            resq._result = req._x % req._y;
        break;
    default:
        resq._code=3;
        break;
    }

    return resq;
}

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        usage(argv[0]);
        exit(USAGE_ERR);
    }
    uint16_t port = atoi(argv[1]);

    unique_ptr<TcpServer> tsvr(new TcpServer(port, calculate));
    tsvr->InitServer();
    tsvr->Start();

    return 0;
}